<?php
/**
 * Created by PhpStorm.
 * User: ice
 * Date: 2015/7/26
 * Time: 14:48
 */

namespace Apps\Manage\Controllers;


use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionAnswer;
use Apps\Common\Models\QuestionTag;
use Apps\Common\Models\SysOpenCity;
use Apps\Common\Models\CosPointquestion;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Libs\UploadPic;
use Apps\Common\Models\BeautyParlorTag;
use Apps\Common\Models\UserBase;
use Apps\Common\Models\BeautyParlorTagInfo;
use Apps\Common\Models\QuestionHotWords;
use Apps\Common\Models\CosQKeyword;

/**
 * 问题管理
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class QuestionController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('question');
    }
    /**
     * 开通的城市
     * @return \Phalcon\Mvc\Model\Criteria
     */
    private function openCityList()
    {
    	return SysOpenCity::find("city_state = 1");
    }
    public function indexAction()
    {
        $this->response->redirect('question/list');
    }
    /*
     * 首页推荐列表
     *   */
    public function question_recommend_listAction(){
        $this->setLeftNav('thumbs');
        $req=$this->request;
        $filterCity = intval($req->getQuery('filterCity',null, -1));
        $filterContent = $req->getQuery('qcontent', null ,'');
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);
        if ($filterContent!='') {
        	$where[]="question_content like '%$filterContent%'";
        }
        if($filterCity>-1){
        	$where[]="addr = $filterCity";
        }
        $whereStr=implode(" and ",$where);
        $columns=[
        'id',
        'q_id',
        'img_url',
        'addr',
        'sort',
        'question_content'
        		];
        $count=CosPointquestion::query()
        ->columns($columns)
        ->leftJoin('Apps\Common\Models\Question','q_id = qa.question_id','qa')
        ->where($whereStr)
        ->execute()
        ->count();
        $list=CosPointquestion::query()
        ->columns($columns)
        ->leftJoin('Apps\Common\Models\Question','q_id = qa.question_id','qa')
        ->where($whereStr)
        ->limit($limit, $offset)
        ->execute()
        ->toArray();
        foreach ($list as $k=>$v){
        	if(!empty($list[$k]['addr']) && $list[$k]['addr']!=0){
        		$tmp=SysOpenCity::findFirst("city_id = ".$list[$k]['addr'])->toArray();
        		$list[$k]['city_name']=$tmp['city_name'];
        	}else{
        		$list[$k]['city_name']='全国';
        	}
        } 
        $this->view->setVar("qcontent",$filterContent);
        $this->view->setVar("total",$count);
        $this->view->setVar("limit",$limit);
        $this->view->setVar("page",$page);
        $this->view->setVar("list",$list);
        $this->view->setVar('filterCity', $filterCity);
        $this->view->setVar('openCity', $this->openCityList());
        $this->view->pick("question_recommend/list");
    }
    /*
     * 取消首页推荐
     *   */
    public function calloffAction(){
    	$req=$this->request;
    	$id=$req->getQuery("id");
    	$cityid=$req->getQuery("city_id");
    	$flg=CosPointquestion::find("id = $id")->delete();
    	if($flg==true){
    		//echo "<script>location.href='/manage/om/bannerBp?City=$cityid'</script>";
    		echo "<script>location.href='/manage/question/question_recommend_list?filterCity=$cityid'</script>";
    	}
    }
    	/*
    	 * 添加首页推荐
    	*   */
    public function question_recommend_addAction(){
        $this->setLeftNav('thumbs');
        $req=$this->request;
        if(!$req->isPost()){
        	$this->view->setVar('question_id',$req->getQuery('id',null,''));
        	$this->view->setVar('openCity', $this->openCityList());
        	$this->view->pick("question_recommend/add");
        	return;
        }
        
        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
        $response->callbackJavascriptTag = true;
        $uploadFile->request = $req;
       	
        
        $q_id = intval($req -> getPost("question_id",null,0)); 
        $addr = intval($req -> getPost("filterCity",null,0));
        if($q_id < 0 || $addr < 0){
        	$response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数错误！');
        	return $response;
        }
        //判断推荐的问题是否存在
        if(Question::count("question_id = ".$q_id)==0){
        	$response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '问题不存在！');
        	return $response;
        }
        //判断问题是否已经推荐
        $qCount=CosPointquestion::count("q_id = $q_id and addr = $addr");
        if($qCount!=0){
        	$response->sendError(ResponseResultStatus::BUSINESS, '此问题已推荐！');
        	return $response;
        }
             
        $sort = CosPointquestion::count("addr = $addr");
        
        $m=new CosPointquestion();
        $m->q_id=$q_id;
        $m->addr=$addr;
        $m->sort=$sort+1;
        $id=$m->save();
        
        if($id===false){
        	$response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '添加失败！');
        }else{
        	$response->sendResult($id);
        }               	
        return $response;
    }

    /**
     * 问题列表
     * @date: 2016年1月19日 
     * @author: futao
     */
    public function listAction()
    {
        $this->setLeftNav('list');
        $req = $this->request;
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);
		
        $filterUser = $req->getQuery('filterUser', null, '');
        $filterState = intval($req->getQuery('filterState', null, -10000));
		$filtertime = $req->getQuery('inputtime' , null , '');
        $filtercontent= $req->getQuery('qcontent' , null ,'');
        $filtertag = $req->getQuery('qtag' ,null ,'');
        $filtercity = $req->getQuery('cityCode' ,null,0);
        $filterNew = $req->getQuery('new',null,-1);
        $cityId = $req->getQuery('city_id',null,0);
        
        $columns = [
            'Apps\Common\Models\Question.question_id',
            'ub.user_id',
            'ub.user_nickname',
            'ub.user_cover',
            'Apps\Common\Models\Question.question_tag',
            'Apps\Common\Models\Question.question_content',
            'Apps\Common\Models\Question.question_pics',
            'Apps\Common\Models\Question.answer_num',
            'Apps\Common\Models\Question.question_follows',
            'Apps\Common\Models\Question.question_browsers',
            'Apps\Common\Models\Question.question_state',
            'Apps\Common\Models\Question.question_addtime',
            'Apps\Common\Models\Question.city_code'
        ];
        $where = [];
        $bind = [];
        //筛选数据
        if($filtercity == -1){
        	$where[] = 'city_code <> 0 ';
        	$this->setLeftNav('citylist');
        }
        if($cityId > 0){
            $where[]= 'Apps\Common\Models\Question.city_code = :city_code:';
            $bind['city_code']=$cityId;
        }
        if($filterNew>-1){
        	$where[]= 'Apps\Common\Models\Question.is_manager_reply = :new:';
        	$bind['new']=$filterNew;
        }
        if($filtertag!=''){
        	$questionIds = QuestionTag::query()->columns("question_id")->where('tag_name like "%'.$filtertag.'%"')->execute()->toArray();
        	if($questionIds!=''){
        		foreach ($questionIds as $val){
        			$ids[]=$val['question_id'];
        		}
        		$ids=implode(",",$ids);
        		$where[]='Apps\Common\Models\Question.question_id in ('.$ids.')';
        	}
        }
        if($filtertime !=''){
        	if(explode("至",$filtertime)){
        		$timearr[]=explode("至",$filtertime);
        		$start_time = $timearr[0][0];
        		$end_time = $timearr[0][1];
        		if($start_time!='' && $end_time!=''){
        			$where[]='Apps\Common\Models\Question.question_addtime >= :start_time: and Apps\Common\Models\Question.question_addtime <= :end_time:';
        			$bind['start_time']=$start_time;
        			$bind['end_time']=$end_time;
        		}
        	}
        } 
        if($filtercontent!=''){
        	$where[]= 'Apps\Common\Models\Question.question_content like :content:';
        	$bind['content']='%'.$filtercontent.'%';
        }
        if($filterUser != '') {
            $where[] = 'ub.user_nickname like :username:';
            $bind['username'] = '%'.$filterUser.'%';
        }
        if($filterState > -10000) {
            $where[] = 'Apps\Common\Models\Question.question_state = :state:';
            $bind['state'] = $filterState;
        }
        $whereStr = implode(' AND ', $where);
        $query = Question::query()
            ->columns($columns)
            ->leftJoin('Apps\Common\Models\UserBase', 'ub.user_id = Apps\Common\Models\Question.user_id', 'ub');
        if(count($where) > 0) {
            $query = $query->where($whereStr)->bind($bind);
        }
        $queryCount = clone $query;
        $data = $query
            ->orderBy('Apps\Common\Models\Question.question_id DESC')
            ->limit($limit, $offset)
            ->execute()->toArray();
        $total = count($queryCount->execute()->toArray());
        foreach ($data as $k =>$v){
            //问题内容解码
            if (unserialize(base64_decode($v['question_content']))){
                $data[$k]['question_content'] = unserialize(base64_decode($v['question_content']));
            }
            if (json_decode($data[$k]['question_tag'])) {
            	$data[$k]['question_tag']=json_decode($data[$k]['question_tag']);
            }
        }
        $this->view->setVar('data', $data);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('total', $total);
        $this->view->setVar('openCity', $this->openCityList());
        
        $this->view->setVar('city_id',$cityId);
        $this->view->setVar('filterNew',$filterNew);
        $this->view->setVar('cityCode' , $filtercity);
        $this->view->setVar('inputtime' , $filtertime);
        $this->view->setVar('filterUser', $filterUser);
        $this->view->setVar('filtertag', $filtertag);
        $this->view->setVar('filterState', $filterState);
        $this->view->setVar('qcontent',$filtercontent);
    }

    /**
     * 删除\恢复问题
     * @date: 2016年1月19日 
     * @author: futao
     */
    public function setStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if($state == -1){
            	$info=CosPointquestion::findFirst("q_id = $id");
            	if($info){
            		return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '已推荐首页！');
            	}
            }
            if(!in_array($state, [-1, 0, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = Question::findFirst('question_id=' . $id);
            if($info) {
                $info->update(['question_state' => $state]);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }

    /**
     * 答案列表
     * @date: 2016年1月19日 
     * @author: futao
     */
    public function answerListAction()
    {
        $this->setLeftNav('answer_list');
        $req = $this->request;
        $id = intval($req->getQuery('id', null, 0));
        if($id <= 0) {
            $this->view->disable();
            echo '问题不存在！';
            return;
        }
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);

        $columns = [
            'question_id',
            'answer_id',
            'ub.user_id',
            'user_nickname',
            'user_cover',
            'answer_floor',
            'answer_content',
            'answer_pics',
            'answer_follows',
            'answer_state',
            'answer_addtime'
        ];
        $list = QuestionAnswer::query()
            ->columns($columns)
            ->leftJoin('Apps\Common\Models\UserBase', 'ub.user_id = Apps\Common\Models\QuestionAnswer.user_id', 'ub')
            ->where('question_id = :id:', ['id' => $id])
            ->orderBy('answer_id ASC')
            ->limit($limit, $offset)
            ->execute()->toArray();
        foreach ($list as $k=>$v){
            //答案解码
            if (unserialize(base64_decode($list[$k]['answer_content']))){
                $list[$k]['answer_content'] = unserialize(base64_decode($list[$k]['answer_content']));
            }
        }
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('total', QuestionAnswer::count('question_id = '. $id));

        $this->view->setVar('data', $list);
        $this->view->setVar('id', $id);
    }

    /**
     * 删除回答
     * @date: 2016年1月19日 
     * @author: futao
     */
    public function setAnswerStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if(!in_array($state, [-1, 0, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = QuestionAnswer::findFirst('answer_id=' . $id);
            if($info) {
                $info->update(['answer_state' => $state]);
                // 记录回答数
                Question::updateAnswerNum($info->question_id);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }
    /**
     * 修改问题
     * @date: 2016-1-12 
     * @author: futao
     */
    public function editAction(){
    	$this->setLeftNav('edit');
    	$req=$this->request;
    	if(!$req->isPost()){
	    	$id = intval($req->getQuery('id', null ,0));
	    	if($id!=''){
	    		$info = Question::findFirst('question_id = '.$id.'');
	    	}
	    	if(!$info){
	    		echo "无效！";
	    		exit;
	    	}
	    	$user=UserBase::findFirst('user_id ='.$info->user_id);
	    	if(unserialize(base64_decode($info->question_content))){
	    		$info->question_content=unserialize(base64_decode($info->question_content));
	    	}
	    	if(json_decode($info->question_pics)){
	    		$info->question_pics=json_decode($info->question_pics);
	    	}
	    	//全部标签
	    	$allTag = BeautyParlorTagInfo::find("parent_id != 0");
	    	//已选择标签
	    	$questionTag=QuestionTag::find('question_id = '.$info->question_id);
	    	//选择的标签转换成字符串
	    	foreach ($questionTag as $k=>$v){
	    		if($k==0){
	    			$tagIds=$v->tag_id;
	    		}else{
	    			$tagIds=$tagIds.",".$v->tag_id;
	    		}
	    	}
	    	$this->view->setVar('question_tag',$questionTag);
	    	$this->view->setVar('alltag',$allTag);
	    	$this->view->setVar('user',$user);
	    	$this->view->setVar('info',$info);
	    	$this->view->setVar('tagids',$tagIds);
	    	return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$question_id = $req->getPost('question_id' ,null ,0);
    	$question_browsers = $req->getPost('question_browsers' , null ,0);
    	$questionTag = $req->getPost('question_tag' , null ,'');
    	//修改标签
        QuestionTag::find("question_id = $question_id")->delete();
    	if($questionTag!='' && is_array($questionTag)){
    		foreach ($questionTag as $val){
    			unset($tagId);
    			unset($tagName);
    			$tagArr = explode(',',$val);
    			$tagId = $tagArr[0];
    			$tagName = $tagArr[1];
    			$tagNameArr[]=$tagName;
    			$questionTagModel = new QuestionTag();
    			$questionTagModel->question_id=$question_id;
    			$questionTagModel->tag_id=$tagId;
    			$questionTagModel->tag_name=$tagName;
    			$questionTagModel->save();
    		}
    	}
    	$allTag = BeautyParlorTagInfo::find("parent_id != 0")->toArray();
    	if($question_id > 0 && $question_browsers >= 0) {
            $questionModel = Question::findFirst("question_id = $question_id and question_state = 1");
            //////////////////////////question_tag字段重新赋值
            $tagList = QuestionTag::find("question_id = {$question_id}")->toArray();
            if (count($tagList) > 0) {
                if (json_decode($tagList)) {
                    $questionTagArr = json_decode($tagList);
                    foreach ($allTag as $k => $v) {
                        foreach ($questionTagArr as $key => $val) {
                            if ($questionTagArr[$key] == $allTag[$k]['tag_title']) {
                                unset($questionTagArr[$key]);
                            }
                        }
                    }
                }
                $questionTagArr = array_values($questionTagArr);
                foreach ($tagNameArr as $v) {
                    $questionTagArr[] = $v;
                }
                $questionTagArr = json_encode(array_unique($questionTagArr));
            }else{
                $questionTagArr = '[]';
            }
    		////////////////////////////////////////////////////////////
    		$questionModel->question_tag=$questionTagArr;
    		$questionModel->question_browsers=$question_browsers;
    		if($questionModel->save()){
    			$echo("修改成功！",true);
    		}else{
    			$echo("修改失败！");
    		}
    	}else{
    		$echo("参数错误！");
    	}
    }
    /**
     * 搜索热词列表
     * @date: 2016-1-14 
     * @author: futao
     */
    public function hotlistAction(){
    	$this->setLeftNav('hotlist');
    	$req = $this->request;
    	$page = intval($req->getQuery('page', null, 1));
    	$page = $page > 0 ? $page : 1;
    	$limit = $this->pageNavLimit;
    	$offset = $limit * ($page - 1);
    	$filterWord = $req->getQuery('keyword' , null ,'');
    	$filterstate = $req->getQuery('status' , null ,'');
    	if($filterWord != ''){
    		$where[] = 'title like "%'.$filterWord.'%"';
    	}
    	if($filterstate !=''){
    		$where[] = 'status = '.$filterstate;
    	}
    	if(count($where)>0){
    		$whereStr = implode(" and ",$where);
    		$wordModel=QuestionHotWords::query()
    		->where($whereStr);
    	}else{
    		$wordModel=QuestionHotWords::query();
    		
    	}
    	$list=$wordModel
    		->limit($limit,$offset)
    		->orderBy('id desc')
    		->execute()->toArray();
    	$total=QuestionHotWords::count($whereStr);
    	$this->view->setVar('state',$filterstate);
    	$this->view->setVar('keyword',$filterWord);
    	$this->view->setVar('list',$list);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('total', $total);
    }
    /**
     * 编辑搜索热词
     * @date: 2016-1-14 
     * @author: futao
     */
    public function hoteditAction(){
    	$this->setLeftNav('hotedit');
    	$req=$this->request;
    	if(!$req->isPost()){
    		$id=$req->getQuery('id', null ,0);
    		if($id>0){
    			$info = QuestionHotWords::findFirst($id);
    		}else{
    			echo "error";
    			return;
    		}
    		$this->view->setVar("info",$info);
    		return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$title = $req->getPost("title" ,null ,'');
    	$id = $req->getPost("id" ,null ,0);
    	if($id < 1){
    		$echo("参数错误！");
    		return;
    	}
    	$hotModel = QuestionHotWords::findFirst($id);
    	$hotModel->title = $title;
    	if($hotModel->save()){
    		$echo('编辑成功！',true);
    		return;
    	}else{
    		$echo('编辑失败！');
    		return;
    	}
    }
    /**
     * 添加搜索热词
     * @date: 2016-1-14 
     * @author: futao
     */
    public function hotaddAction(){
    	$this->setLeftNav("hotadd");
    	$req = $this->request;
    	if(!$req->isPost()){
    		$this->view->pick('question/hotedit');
    		return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$title = $req->getPost("title" , null ,'');
    	if($title!=''){
    		$hotModel = new QuestionHotWords();
    		$hotModel->title = $title;
    		$hotModel->status = 1;
    		if($hotModel->save()){
    			$echo('添加成功！',true);
    		}else{
    			$echo("添加失败！");
    		}
    	}else{
    		$echo("添加失败！");
    	}
    }
    /**
     *  改变状态
     * @date: 2016-1-14 
     * @author: futao
     */
    public function changestateAction(){
    	$req = $this->request;
    	$state = $req->getQuery('state' , null ,'');
    	$id = $req->getQuery('id' , null , 0);
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	if(in_array($state,[1,0]) && $id > 0){
    		$hotModel = QuestionHotWords::findFirst($id);
    		$hotModel->status = $state;
    		if($hotModel->save()){
    			 echo "<script>window.parent.location.href='hotlist'</script>";
    		}else{
    			$echo("操作失败！");
    		}
    	}else{
    		$echo("操作失败！");
    	}
    }  
    /**
     * 敏感词列表
     * @date: 2016-1-15
     * @author: futao
     */
    public function keywordlistAction(){
    	$this->setLeftNav('keywordlist');
    	$req = $this->request;
    	$page = intval($req->getQuery('page', null, 1));
    	$page = $page > 0 ? $page : 1;
    	$limit = $this->pageNavLimit;
    	$offset = $limit * ($page - 1);
    	$filterWord = $req->getQuery('keyword' , null ,'');
    	if($filterWord != ''){
    		$where[] = 'keyword like "%'.$filterWord.'%"';
    	}
    	if(count($where)>0){
    		$whereStr = implode(" and ",$where);
    		$wordModel=CosQKeyword::query()
    		->where($whereStr);
    	}else{
    		$wordModel=CosQKeyword::query();
    	}
    	$list=$wordModel
    		->limit($limit,$offset)
    		->orderBy('id desc')
    		->execute()->toArray();
    	$total=CosQKeyword::count($whereStr);
    	$this->view->setVar('keyword',$filterWord);
    	$this->view->setVar('list',$list);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        $this->view->setVar('total', $total);
    }
    /**
     * 添加敏感词
     * @date: 2016-1-14
     * @author: futao
     */
    public function keywordaddAction(){
    	$this->setLeftNav("keywordadd");
    	$req = $this->request;
    	if(!$req->isPost()){
    		$this->view->pick('question/keywordedit');
    		return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$title = $req->getPost("keyword" , null ,'');
    	if($title!=''){
    		$hotModel = new CosQKeyword();
    		$hotModel->keyword = $title;
    		if($hotModel->save()){
    			$echo('添加成功！',true);
    		}else{
    			$echo("添加失败！");
    		}
    	}else{
    		$echo("添加失败！");
    	}
    }
    /**
     * 编辑搜索热词
     * @date: 2016-1-14
     * @author: futao
     */
    public function keywordeditAction(){
    	$this->setLeftNav('keywordedit');
    	$req=$this->request;
    	if(!$req->isPost()){
    		$id=$req->getQuery('id', null ,0);
    		if($id>0){
    			$info = CosQKeyword::findFirst($id);
    		}else{
    			echo "error";
    			return;
    		}
    		$this->view->setVar("info",$info);
    		return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$title = $req->getPost("keyword" ,null ,'');
    	$id = $req->getPost("id" ,null ,0);
    	if($id < 1){
    		$echo("参数错误！");
    		return;
    	}
    	$hotModel = CosQKeyword::findFirst($id);
    	$hotModel->keyword = $title;
    	if($hotModel->save()){
    		$echo('编辑成功！',true);
    		return;
    	}else{
    		$echo('编辑失败！');
    		return;
    	}
    }
    /**
     * 敏感词删除
     * @date: 2016-1-15 
     * @author: futao
     */
    public function keyworddeleteAction(){
    	$req = $this->request;
    	$keywordId = $req->getQuery('id' , null , 0);
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	if($keywordId>0){
    		$keywordModel=CosQKeyword::findFirst($keywordId);
    		if($keywordModel){
    			if($keywordModel->delete()){
    				echo "<script>window.parent.location.href='keywordlist'</script>";
    			}
    		}else{
    			$echo("删除失败！");
    		}
    	}else{
    		$echo("删除失败！");
    	}
    } 
    /**
     * 添加问题
     * @date: 2016-1-15 
     * @author: futao
     */
    public function addquestionAction(){
    	$this->setLeftNav('addquestion');
    	$req = $this->request;
    	if(!$req->isPost()){
    		$userList=UserBase::find("is_manage = 1");
    		$tag=BeautyParlorTagInfo::find("parent_id != 0 and tag_state = 1");
    		$this->view->setVar('city_list',$this->openCityList());
    		$this->view->setVar('userlist',$userList);
    		$this->view->setVar('tag',$tag);
    		$this->view->pick("question/add");
    		return;
    	}
    	$this->view->disable();
    	$echo = function($msg, $state=false){
    		echo '<script type="text/javascript">';
    		echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
    		echo '</script>';
    	};
    	$questionContent = $req->getPost('question_content',null,'');
    	$questionTag = $req->getPost('question_tag',null,'');
    	$questionBrowsers = intval($req->getPost('question_browsers',null,0));
    	$userId = intval($req->getPost('user_id',null,0));
    	$cityCode = $req->getPost('city_id',null,0);
    	if($userId<0 || $questionBrowsers<0){
    		$echo("添加失败！");
    		return;
    	}
    	if(is_array($questionTag)){
    		foreach ($questionTag as $val){
    			$tagArr = explode(',',$val);
    			$tagName = $tagArr[1];
    			$tagNameArr[]=$tagName;	
    		}
    		$tags = json_encode($tagNameArr);
    	}else{
    		$tags = '[]';
    	}
    	$questionModel=new Question();
    	//保存图片
    	if($_FILES['q_photos']['name'][0]!=''){
    		$uploadFile = new UploadPic();
        	$uploadFile->request = $req;
        	$questionPics = $uploadFile->questionPic();
        	$questionModel->question_pics=json_encode($questionPics);
    	}else{
    		$questionModel->question_pics=json_encode([]);
    	}
		$questionModel->question_content=$questionContent;
		$questionModel->city_code=$cityCode;
    	$questionModel->question_browsers=$questionBrowsers;
    	$questionModel->question_tag=$tags;
    	$questionModel->user_id = $userId;
    	if(!$questionModel->save()){
    		$echo("添加失败！");
    		return;
    	}
    	if($questionTag!='' && is_array($questionTag)){
    		unset($tagNameArr);
    		unset($tagName);
    		unset($tagArr);
    		foreach ($questionTag as $val){
    			unset($tagId);
    			unset($tagName);
    			$tagArr = explode(',',$val);
    			$tagId = $tagArr[0];
    			$tagName = $tagArr[1];
    			$tagNameArr[]=$tagName;
    			$questionTagModel = new QuestionTag();
    			$questionTagModel->question_id=$questionModel->question_id;
    			$questionTagModel->tag_id=$tagId;
    			$questionTagModel->tag_name=$tagName;
    			if($questionTagModel->save()){
    				$echo("添加成功！",true);
    			}
    		}
    	}else{
    		$echo("添加成功！",true);
    		return;
    	}
    }


    
    
    
}